<template>
  <div class="longins">
    <div class="login-main">
      <p class="title">登录</p>
      <el-form
        :model="ruleForm"
        :rules="rules"
        label-width="80px"
        ref="ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username" type="text" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" type="password" />
        </el-form-item>

        <el-button type="primary" @click="submitForm('ruleForm')"
          >登录</el-button
        >
      </el-form>
    </div>
  </div>
</template>
<script>
import { Login } from "@/http/login.js";
import { mapMutations } from "vuex";
export default {
  name: "APP",
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          {
            required: true,
            min: 2,
            max: 5,
            message: "字符长度在2到5之间",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码，该项为必填项",
            trigger: "blur",
          },
        ],
        // age: [{ validator: checkAge, trigger: "blur" }],
      },
    };
  },
  methods: {
    ...mapMutations("login", ["setlogin"]),
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert("submit!");
          console.log(this.ruleForm);

          Login({
            user: this.ruleForm.username,
            pwd: this.ruleForm.password,
          }).then((res) => {
            console.log(res);
            if (res.data.status == 200) {
              this.setlogin({
                username: this.ruleForm.username,
                role: res.data.token,
              });
              this.$router.push("/");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  created() {},
  mounted() {},
};
</script>
<style lang="scss" scoped>
.longins {
  width: 100%;
  height: 100%;
  background: rgba(64, 64, 194, 0.1);
  border: 1px solid transparent;
}
.login-main {
  width: 600px;
  height: 300px;
  // background-color: aqua;
  margin: 300px auto 0;
  display: flex;
  flex-direction: column;
  // justify-content: space-between;
  align-content: center;
  .title {
    width: 100%;
    height: 50px;
    text-align: center;
    font-size: 22px;
    font-weight: bold;
  }
}
</style>
